package com.gaff.emp.core.modules.perm;

import com.gaff.emp.core.modules.perm.entity.SysMenu;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class MenuUtil {

    public static List<SysMenu> mergeMenus(List<SysMenu> menus) {
        List<SysMenu> menuResult = new ArrayList<>();
        for (SysMenu menu : menus) {
            if (menu.getType() == SysMenu.catalog) {
                List<SysMenu> subMenus = new ArrayList<>();
                for (SysMenu subMenu : menus) {
                    if (subMenu.getType() == SysMenu.menu && subMenu.getParentId() == menu.getId()) {
                        subMenu.setParentName(menu.getName());
                        subMenus.add(subMenu);
                    }
                }
                subMenus.sort(Comparator.comparingInt(SysMenu::getSort));
                menu.setSubMenus(subMenus);
                menuResult.add(menu);
            }
        }
        menuResult.sort(Comparator.comparingInt(SysMenu::getSort));
        return menuResult;
    }

}
